package com.shushan.home;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.android.common.ui.RefreshLayout;
import com.android.common.ui.UnScrollListView;
import com.android.common.ui.banner.Banner;
import com.android.common.ui.banner.BannerConfig;
import com.android.common.ui.banner.listener.OnBannerListener;
import com.android.fingerprintidentify.FingerprintIdentify;
import com.android.fingerprintidentify.base.BaseFingerprint;
import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.android.permission.rom.RomType;
import com.android.permission.rom.RomUtils;
import com.andview.refreshview.XRefreshView;
import com.shushan.accessibility.PermissionAccessOpenActivity;
import com.shushan.accessibility.PermissionHandOpenActivity;
import com.shushan.base.ActivityStackManager;
import com.shushan.base.BaseFragment;
import com.shushan.base.BaseHomeFragment;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.chant.PEPChant;
import com.shushan.home.adapter.TodayStatisAdapter;
import com.shushan.home.bean.BannerDataBean;
import com.shushan.home.bean.IndexBean;
import com.shushan.home.bean.UseSatistBean;
import com.shushan.manager.JobSchedulerManager;
import com.shushan.manager.SoftWareMgActivity;
import com.shushan.manager.TimeManagerActivity;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.pages.InviteForFreeUseActivity;
import com.shushan.pages.OpenVipSpendActivity;
import com.shushan.pages.SpecialTopicVideoActivity;
import com.shushan.service.DaemonService;
import com.shushan.service.ForegroundRTAService;
import com.shushan.service.PlayerMusicService;
import com.shushan.setting.ParentSettingActivity;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.BuildConfig;
import com.shushan.shushanbao.R;
import com.shushan.ui.CustomDialog;
import com.shushan.ui.RefreshHeader;
import com.shushan.ui.VeriCodeNoView;
import com.shushan.ui.VerificationCodeView;
import com.shushan.util.ControlUtil;
import com.shushan.util.DateUtils;
import com.shushan.util.GlideImageLoader;
import com.shushan.util.ImageUtils;
import com.shushan.util.KeyboardUtils;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.StringTools;
import com.shushan.util.SystemUtils;
import com.shushan.util.ToolUtils;
import com.shushan.util.UmengEventUtil;
import com.shushan.web.AdvertWebActivity;
import com.shushan.web.ParentSetWebActivity;

import java.util.ArrayList;
import java.util.List;

import static com.android.permission.rom.RomType.FUNTOUCHOS_ROM;

/**
 * @ClassName: ParentFragmet
 * @Desciption: 家长模式fragment
 * @author: yichaohua
 * @date: 2018-07-16
 */
public class ParentFragmet extends BaseHomeFragment implements View.OnClickListener, VerificationCodeView.OnCodeFinishListener {

    private static final String TAG = "ParentFragmet";

    private LinearLayout softwareMgLayout;
    private LinearLayout timeMgLayout;
    private LinearLayout exitControlLayout;
    private Switch goSoftSwitch;
    private Switch allowPlaySwitch;
    private Switch projectEyeSwitch;
    private Switch allowConvertSwitch;
    private View goSoftView;
    private View allowPlayView;
    private View projectEyeView;
    private View allowConvertView;
    private View view;
    private Dialog softSwitchDialog;
    private Dialog playSwitchDialog;
    private Dialog eyeSwitchDialog;
    private Dialog convertSwitchDialog;
    private Dialog noVipDialog;
    private TextView conrolText;
    private RelativeLayout controlLayout;
    private RelativeLayout vercodeLayout;
    private FingerprintIdentify mFingerprintIdentify;
    private Banner mBanner;
    private VeriCodeNoView veriCodeView;
    private Button button1,button2,button3,button4,button5,button6,
            button7,button8,button9,button0;
    private ImageButton delImagButton;
    private ImageView controlImg;

    private BabyApplication shuShanApplication = BabyApplication.getInstance();
    private List<BannerDataBean> bannerDataBeanList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        Log.i(TAG,"onCreateView()");

        view = inflater.inflate(R.layout.parent_fragment, null);
        ScreenManager sm = new ScreenManager();
        if(!sm.setDeepStatusBar(true,getActivity())){
            view.findViewById(R.id.titlebar_divider).setVisibility(View.GONE);
        }

        initView(view);

        //初始化广告栏
        initBanner();

        getData();

        boolean isEyeOpen = SharedPreferencesUtils.getBooleanDate(Constants.PROJECT_EYE_SWITCH_SHAREPREFERS_KEY);
        if(isEyeOpen){
            ControlUtil.cancelProjectEyeAlarm(getActivity());
        }

        showPswDialog();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        Log.i(TAG,"onResume()");

        boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
        boolean permissionHasSet = SharedPreferencesUtils.getBooleanDate(Constants.MAIN_PERMISSION_HAS_SET_SHAREPREFERS_KEY);
        Log.i(TAG,"isChildControl=" + isChildControl + " permissionHasSet=" + permissionHasSet);
        if(!permissionHasSet){
            conrolText.setText("开启管控");
            controlImg.setImageResource(R.drawable.control_icon);
        }else if(isChildControl){
            conrolText.setText("关闭管控");
            controlImg.setImageResource(R.drawable.control_icon_off);
        }else {
            conrolText.setText("开启管控");
            controlImg.setImageResource(R.drawable.control_icon);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if(mFingerprintIdentify != null){
            mFingerprintIdentify.cancelIdentify();
        }

        mBanner.stopAutoPlay();

        closeDialog();
    }

    private void initBanner(){
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR).setImageLoader(new GlideImageLoader());
        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if(ToolUtils.isListEmpty(bannerDataBeanList)){
                    return;
                }else{
                    BannerDataBean dataBean = bannerDataBeanList.get(position);
                    bannerJump(dataBean);
                }
            }
        });
    }

    private void initView(View view) {
        button0 = view.findViewById(R.id.pin_lock_screen_0_btn);
        button0.setOnClickListener(this);
        button1 = view.findViewById(R.id.pin_lock_screen_1_btn);
        button1.setOnClickListener(this);
        button2 = view.findViewById(R.id.pin_lock_screen_2_btn);
        button2.setOnClickListener(this);
        button3 = view.findViewById(R.id.pin_lock_screen_3_btn);
        button3.setOnClickListener(this);
        button4 = view.findViewById(R.id.pin_lock_screen_4_btn);
        button4.setOnClickListener(this);
        button5 = view.findViewById(R.id.pin_lock_screen_5_btn);
        button5.setOnClickListener(this);
        button6 = view.findViewById(R.id.pin_lock_screen_6_btn);
        button6.setOnClickListener(this);
        button7 = view.findViewById(R.id.pin_lock_screen_7_btn);
        button7.setOnClickListener(this);
        button8 = view.findViewById(R.id.pin_lock_screen_8_btn);
        button8.setOnClickListener(this);
        button9 = view.findViewById(R.id.pin_lock_screen_9_btn);
        button9.setOnClickListener(this);
        delImagButton = view.findViewById(R.id.pin_lock_screen_del_img);
        delImagButton.setOnClickListener(this);
        vercodeLayout = view.findViewById(R.id.parent_fragment_vercode_layout);
        veriCodeView = view.findViewById(R.id.verification_code_view);
        veriCodeView.setOnCodeFinishListener(this);
        mBanner = view.findViewById(R.id.banner);
        controlLayout = view.findViewById(R.id.parent_control_layout);
        controlLayout.setOnClickListener(this);
        conrolText = view.findViewById(R.id.parent_control_tv);
        goSoftView = view.findViewById(R.id.parent_goto_soft_layout);
        goSoftView.setOnClickListener(this);
        allowPlayView = view.findViewById(R.id.parent_allow_play_layout);
        allowPlayView.setOnClickListener(this);
        projectEyeView = view.findViewById(R.id.parent_project_eye_layout);
        projectEyeView.setOnClickListener(this);
        allowConvertView = view.findViewById(R.id.parent_allow_convert_layout);
        allowConvertView.setOnClickListener(this);
        goSoftSwitch = view.findViewById(R.id.parent_goto_soft_switch);
        goSoftSwitch.setChecked(SharedPreferencesUtils.getBooleanDates(Constants.GO_SOFT_SWITCH_SHAREPREFERS_KEY));
        allowPlaySwitch = view.findViewById(R.id.parent_allow_play_switch);
        allowPlaySwitch.setChecked(SharedPreferencesUtils.getBooleanDates(Constants.ALLOW_PLAY_SWITCH_SHAREPREFERS_KEY));
        projectEyeSwitch = view.findViewById(R.id.parent_project_eye_switch);
        projectEyeSwitch.setChecked(SharedPreferencesUtils.getBooleanDate(Constants.PROJECT_EYE_SWITCH_SHAREPREFERS_KEY));
        allowConvertSwitch = view.findViewById(R.id.parent_allow_convert_switch);
        allowConvertSwitch.setChecked(SharedPreferencesUtils.getBooleanDate(Constants.ALLOW_CONVERT_SWITCH_SHAREPREFERS_KEY));
        softwareMgLayout = view.findViewById(R.id.parent_software_mg_layout);
        softwareMgLayout.setOnClickListener(this);
        timeMgLayout = view.findViewById(R.id.parent_time_mg_layout);
        timeMgLayout.setOnClickListener(this);
        exitControlLayout = view.findViewById(R.id.parent_main_exit_control_layout);
        exitControlLayout.setOnClickListener(this);
        controlImg = view.findViewById(R.id.parent_control_img);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.parent_software_mg_layout:
                startActivity(SoftWareMgActivity.class);
                break;
            case R.id.parent_time_mg_layout:
                startActivity(TimeManagerActivity.class);
                break;
            case R.id.parent_goto_soft_layout:
                showSoftSwitchDialog();
                break;
            case R.id.parent_allow_play_layout:
                showPlaySwitchDialog();
                break;
            case R.id.parent_project_eye_layout:
                showEyeSwitchDialog();
                break;
            case R.id.parent_allow_convert_layout:
                showConvertSwitchDialog();
                break;
            case R.id.parent_main_exit_control_layout:
                setAllowBack(true);
                ActivityStackManager.getActivityStackManager().popAllActivity();
                SharedPreferencesUtils.setBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY,false);

                showToast("返回桌面成功");

                Intent intent = new Intent();
                intent.setAction(Constants.STOP_DAEMON_RECEIVER_ACTION);
                getActivity().sendBroadcast(intent);

                Intent intent1 = new Intent();
                intent1.setAction(Constants.STOP_PLAY_RECEIVER_ACTION);
                getActivity().sendBroadcast(intent1);

                if("oppo".equals(BuildConfig.ENVIRONMENT)){
                    System.exit(0);
                }
                break;
            case R.id.parent_control_layout:
                onControl();
                break;
            case R.id.pin_lock_screen_0_btn:
                veriCodeView.setText(0);
                break;
            case R.id.pin_lock_screen_1_btn:
                veriCodeView.setText(1);
                break;
            case R.id.pin_lock_screen_2_btn:
                veriCodeView.setText(2);
                break;
            case R.id.pin_lock_screen_3_btn:
                veriCodeView.setText(3);
                break;
            case R.id.pin_lock_screen_4_btn:
                veriCodeView.setText(4);
                break;
            case R.id.pin_lock_screen_5_btn:
                veriCodeView.setText(5);
                break;
            case R.id.pin_lock_screen_6_btn:
                veriCodeView.setText(6);
                break;
            case R.id.pin_lock_screen_7_btn:
                veriCodeView.setText(7);
                break;
            case R.id.pin_lock_screen_8_btn:
                veriCodeView.setText(8);
                break;
            case R.id.pin_lock_screen_9_btn:
                veriCodeView.setText(9);
                break;
            case R.id.pin_lock_screen_del_img:
                veriCodeView.setText(-1);
                break;
            default:
                break;
        }
    }

    private void onControl() {
        boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
        boolean permissionHasSet = SharedPreferencesUtils.getBooleanDate(Constants.MAIN_PERMISSION_HAS_SET_SHAREPREFERS_KEY);
        if(!permissionHasSet){
            RomType romType = RomUtils.getRomType(getActivity());
            Log.i(TAG,"romType=" + romType + " Build.VERSION.SDK_INT=" + Build.VERSION.SDK_INT);
            if(romType == FUNTOUCHOS_ROM && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                startActivity(PermissionHandOpenActivity.class);
            }else {
                startActivity(PermissionAccessOpenActivity.class);
            }

        }else if(isChildControl){
            showCloseControlDialog();
        }else {
            SharedPreferencesUtils.setBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY,true);
            Intent intent = new Intent(getActivity(), DaemonService.class);
            getActivity().startService(intent);

            Intent intent1 = new Intent(getActivity(),PlayerMusicService.class);
            getActivity().startService(intent1);

            //启动系统任务
            JobSchedulerManager mJobManager = JobSchedulerManager.getJobSchedulerInstance(shuShanApplication);
            mJobManager.startJobScheduler();

            conrolText.setText("关闭管控");

            showToast("开启管控成功");
            controlImg.setImageResource(R.drawable.control_icon_off);
        }

    }

    private void showCloseControlDialog() {
        new CustomDialog.Builder(getActivity())
                .setTitle("温馨提示")
                .setMessage("您是否确认关闭管控？")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        SharedPreferencesUtils.setBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY,false);

                        Intent intent = new Intent();
                        intent.setAction(Constants.STOP_DAEMON_RECEIVER_ACTION);
                        getActivity().sendBroadcast(intent);

                        Intent intent1 = new Intent();
                        intent1.setAction(Constants.STOP_PLAY_RECEIVER_ACTION);
                        getActivity().sendBroadcast(intent1);

                        //启动系统任务
                        JobSchedulerManager mJobManager = JobSchedulerManager.getJobSchedulerInstance(shuShanApplication);
                        mJobManager.stopJobScheduler();

                        conrolText.setText("开启管控");
                        controlImg.setImageResource(R.drawable.control_icon);
                    }
                })
                .create().show();
    }

    private void closeDialog() {
        if(noVipDialog != null){
            noVipDialog.dismiss();
        }

        if(softSwitchDialog != null){
            softSwitchDialog.dismiss();
        }

        if(playSwitchDialog != null){
            playSwitchDialog.dismiss();
        }

        if(eyeSwitchDialog != null){
            eyeSwitchDialog.dismiss();
        }

        if(convertSwitchDialog != null){
            convertSwitchDialog.dismiss();
        }
    }

    private void showSoftSwitchDialog() {
        final boolean isChecked = goSoftSwitch.isChecked();

        if(softSwitchDialog == null){
            softSwitchDialog = new Dialog(getActivity(),R.style.Dialog_Translucent_Background);
            softSwitchDialog.setContentView(R.layout.go_soft_dialog);
            Button coloseButton = softSwitchDialog.findViewById(R.id.go_soft_cancel_btn);
            coloseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    softSwitchDialog.dismiss();
                }
            });

            softSwitchDialog.setCancelable(false);
        }

        ImageView titleImg = softSwitchDialog.findViewById(R.id.go_soft_img);
        TextView titleTextView = softSwitchDialog.findViewById(R.id.go_soft_title_tv);
        TextView contentTextView1 = softSwitchDialog.findViewById(R.id.go_soft_content_tv1);
        TextView contentTextView2 = softSwitchDialog.findViewById(R.id.go_soft_content_tv2);
        Button postitveButton = softSwitchDialog.findViewById(R.id.go_soft_postitve_btn);
        if(isChecked){
            titleImg.setImageResource(R.drawable.title_close);
            titleTextView.setText("确定关闭开机默认进入软件?");
            contentTextView1.setText("如果共用手机，关闭该功能后，家长需每次打开软件给孩子；");
            contentTextView2.setText("如果孩子单独使用一个手机，建议不要关闭该功能");
            postitveButton.setText("关闭");
            postitveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    softSwitchDialog.dismiss();

                    SharedPreferencesUtils.setBooleanDate(Constants.GO_SOFT_SWITCH_SHAREPREFERS_KEY,!isChecked);
                    goSoftSwitch.setChecked(!isChecked);
                }
            });
        }else {
            titleImg.setImageResource(R.drawable.title_open);
            titleTextView.setText("确定开启开机默认进入软件?");
            contentTextView1.setText("如果共用手机，开启该功能后，家长无需每次打开软件给孩子；");
            contentTextView2.setText("如果孩子单独使用一个手机，建议开启该功能");
            postitveButton.setText("开启");
            postitveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    softSwitchDialog.dismiss();

                    SharedPreferencesUtils.setBooleanDate(Constants.GO_SOFT_SWITCH_SHAREPREFERS_KEY,!isChecked);
                    goSoftSwitch.setChecked(!isChecked);
                }
            });
        }

        if(!softSwitchDialog.isShowing()){
            softSwitchDialog.show();
        }
    }

    private void showConvertSwitchDialog() {
        final boolean isChecked = allowConvertSwitch.isChecked();

        if(convertSwitchDialog == null){
            convertSwitchDialog = new Dialog(getActivity(),R.style.Dialog_Translucent_Background);
            convertSwitchDialog.setContentView(R.layout.allow_convert_dialog);
            Button coloseButton = convertSwitchDialog.findViewById(R.id.allow_convert_cancel_btn);
            coloseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    convertSwitchDialog.dismiss();
                }
            });

            convertSwitchDialog.setCancelable(false);
        }

        TextView titleTextView = convertSwitchDialog.findViewById(R.id.allow_convert_title_tv);
        TextView contentTextView = convertSwitchDialog.findViewById(R.id.allow_convert_content_tv);
        Button postitveButton = convertSwitchDialog.findViewById(R.id.allow_convert_postitve_btn);
        if(isChecked){
            titleTextView.setText("确定关闭允许学贝兑换玩具");
            contentTextView.setText("关闭学贝兑换玩具，孩子将不可以用学贝在商城免费兑换喜欢的礼物");
            postitveButton.setText("关闭");
            postitveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    convertSwitchDialog.dismiss();

                    SharedPreferencesUtils.setBooleanDate(Constants.ALLOW_CONVERT_SWITCH_SHAREPREFERS_KEY,!isChecked);
                    allowConvertSwitch.setChecked(!isChecked);
                }
            });
        }else {
            titleTextView.setText("确定开启允许学贝兑换玩具");
            contentTextView.setText("开启学贝兑换玩具，孩子可以自己用学贝在商城免费兑换喜欢的礼");
            postitveButton.setText("开启");
            postitveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    convertSwitchDialog.dismiss();

                    SharedPreferencesUtils.setBooleanDate(Constants.ALLOW_CONVERT_SWITCH_SHAREPREFERS_KEY,!isChecked);
                    allowConvertSwitch.setChecked(!isChecked);
                }
            });
        }

        if(!convertSwitchDialog.isShowing()){
            convertSwitchDialog.show();
        }
    }
    private void gotoParentSetWeb(){
        UrlParse urlParse = new UrlParse(UrlConst.BASE_URL)
                .appendRegion(UrlConst.PARENT_SET)
                .putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY));

        super.setAllowBack(true);
        Intent intent = new Intent(getActivity(), ParentSetWebActivity.class);
        intent.putExtra("title","家长设置");
        intent.putExtra("url",urlParse.toString());
        startActivity(intent);
    }

    private void showPlaySwitchDialog() {
        final boolean isChecked = allowPlaySwitch.isChecked();

        if(playSwitchDialog == null){
            playSwitchDialog = new Dialog(getActivity(),R.style.Dialog_Translucent_Background);
            playSwitchDialog.setContentView(R.layout.allow_play_dialog);
            Button coloseButton = playSwitchDialog.findViewById(R.id.allow_play_cancel_btn);
            coloseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    playSwitchDialog.dismiss();
                }
            });

            playSwitchDialog.setCancelable(false);
        }

        ImageView titleImg = playSwitchDialog.findViewById(R.id.allow_play_img);
        TextView titleTextView = playSwitchDialog.findViewById(R.id.allow_play_title_tv);
        TextView contentTextView1 = playSwitchDialog.findViewById(R.id.allow_play_content_tv1);
        Button postitveButton = playSwitchDialog.findViewById(R.id.allow_play_postitve_btn);
        if(isChecked){
            titleImg.setImageResource(R.drawable.title_close);
            titleTextView.setText("确定关闭允许学习换取娱乐时间?");
            contentTextView1.setText("关闭该功能，孩子练习后，将换取不到对应的娱乐时间。");
            postitveButton.setText("关闭");
            postitveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    playSwitchDialog.dismiss();

                    SharedPreferencesUtils.setBooleanDate(Constants.ALLOW_PLAY_SWITCH_SHAREPREFERS_KEY,!isChecked);
                    allowPlaySwitch.setChecked(!isChecked);
                }
            });
        }else {
            titleImg.setImageResource(R.drawable.title_open);
            titleTextView.setText("确定开启允许学习换取娱乐时间?");
            contentTextView1.setText("开启该功能，孩子练习后，将换取到对应的娱乐时间。");
            postitveButton.setText("开启");
            postitveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    playSwitchDialog.dismiss();

                    SharedPreferencesUtils.setBooleanDate(Constants.ALLOW_PLAY_SWITCH_SHAREPREFERS_KEY,!isChecked);
                    allowPlaySwitch.setChecked(!isChecked);
                }
            });
        }

        if(!playSwitchDialog.isShowing()){
            playSwitchDialog.show();
        }
    }

    private void showEyeSwitchDialog() {
        final boolean isChecked = projectEyeSwitch.isChecked();

        if(isChecked){
            SharedPreferencesUtils.setBooleanDate(Constants.PROJECT_EYE_SWITCH_SHAREPREFERS_KEY,false);
            projectEyeSwitch.setChecked(false);
        }else {
            if(eyeSwitchDialog == null){
                eyeSwitchDialog = new Dialog(getActivity(),R.style.Dialog_Translucent_Background);
                eyeSwitchDialog.setContentView(R.layout.project_eye_dialog);

                RadioButton radioButton = eyeSwitchDialog.findViewById(R.id.project_eye_rb);
                final boolean isHalfHour = radioButton.isChecked();


                Button coloseButton = eyeSwitchDialog.findViewById(R.id.project_eye_cancel_btn);
                coloseButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        eyeSwitchDialog.dismiss();
                        ControlUtil.cancelProjectEyeAlarm(getActivity());
                    }
                });

                Button openButton = eyeSwitchDialog.findViewById(R.id.project_eye_open_btn);
                openButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        eyeSwitchDialog.dismiss();

                        SharedPreferencesUtils.setBooleanDate(Constants.PROJECT_EYE_SWITCH_SHAREPREFERS_KEY,true);
                        projectEyeSwitch.setChecked(true);

                        SharedPreferencesUtils.setIntDate(Constants.PROJECT_EYE_INTERVAL_SHAREPREFERS_KEY,isHalfHour?30:60);

                    }
                });

                eyeSwitchDialog.setCancelable(false);
            }

            if(!eyeSwitchDialog.isShowing()){
                eyeSwitchDialog.show();
            }
        }

    }

    private void showNoVipDialog() {
        if(noVipDialog == null){
            noVipDialog = new Dialog(getActivity(),R.style.Dialog_Translucent_Background);
            noVipDialog.setCanceledOnTouchOutside(true);
            noVipDialog.setContentView(R.layout.no_vip_dialog);

            Button openButton = noVipDialog.findViewById(R.id.no_vip_open_btn);
            openButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    noVipDialog.dismiss();

                    startActivity(OpenVipSpendActivity.class);
                }
            });

            Button shareButton = noVipDialog.findViewById(R.id.no_vip_share_btn);
            shareButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    noVipDialog.dismiss();

                    startActivity(InviteForFreeUseActivity.class);
                }
            });

        }

        if(!noVipDialog.isShowing()){
            noVipDialog.show();
        }
    }

    private void getData() {
        showWaitDialog();

        int childId = shuShanApplication.getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(getActivity(), UrlConst.BASE_URL).appendRegion(UrlConst.GET_CHILD_INDEX);
        urlParse.putValue("child_id", childId);

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<IndexBean>() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(IndexBean indexBean) {
                        if(indexBean != null){
                            bannerDataBeanList = indexBean.getBannerDataBeanList();
                            setBanner();
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    private void setBanner(){
        if(ToolUtils.isListEmpty(bannerDataBeanList)){
            Log.d(TAG,"banner数据为空");
            return;
        }
        List<String> images = new ArrayList<String>();
        for(BannerDataBean bean : bannerDataBeanList){
            String loadUrl;
            if (ImageUtils.isAbsoluteUrl(bean.pic)) {
                loadUrl = bean.pic;
            } else {
                loadUrl = BuildConfig.imgBaseUrl + bean.pic;
            }
            images.add(loadUrl);
        }
        mBanner.setImages(images).start();
    }

    //根据type跳转不同界面
    private void bannerJump(BannerDataBean dataBean){
        switch (dataBean.type){
            case 0://跳转H5
                Intent intent2 = new Intent(getActivity(), AdvertWebActivity.class);
                intent2.putExtra("title",dataBean.name);
                intent2.putExtra("url",dataBean.url);
                startActivityAllow(intent2);
                break;
            case 1://跳转APP页面
                if(dataBean.url.equals("0")){
                    startActivity(InviteForFreeUseActivity.class);
                }else if(!TextUtils.isEmpty(dataBean.android_name)){
                    try {
                        Intent intent1 = new Intent();
                        intent1.setClassName(getContext(),dataBean.android_name);
                        startActivityAllow(intent1);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                break;
            case 2://跳转视频页面
                Intent intent = new Intent(getActivity(),SpecialTopicVideoActivity.class);
                intent.putExtra("isOnlyPlay",true);
                intent.putExtra("video_title",dataBean.name);
                intent.putExtra("video_url",dataBean.url);
                startActivityAllow(intent);
                break;
            case 4:
                UmengEventUtil.onEvent(shuShanApplication,"control_101_download_click_id",dataBean.android_name);
                SystemUtils.openAppMarket(getActivity(),dataBean.android_name);
                break;
            default:
                break;
        }
    }

    private void showOpenControlDialog() {
        new CustomDialog.Builder(getActivity())
                .setTitle("温馨提示")
                .setMessage("请开启管控权限，彻底控制小孩子玩手机")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();


                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        startActivity(PermissionAccessOpenActivity.class);
                    }
                })
                .create().show();
    }

    public TextView getConrolText() {
        return conrolText;
    }

    public void setConrolText(TextView conrolText) {
        this.conrolText = conrolText;
    }

    @Override
    public void onFragmentChecked() {
        showPswDialog();

    }

    @Override
    public void onFragmentHided() {
        if(mFingerprintIdentify != null){
            mFingerprintIdentify.cancelIdentify();
        }
    }

    private void showPswDialog(){
        vercodeLayout.setVisibility(View.VISIBLE);
        if(mFingerprintIdentify == null){
            mFingerprintIdentify = new FingerprintIdentify(BabyApplication.getInstance(), new BaseFingerprint.FingerprintIdentifyExceptionListener() {
                @Override
                public void onCatchException(Throwable exception) {
                    Log.e(TAG,exception.getLocalizedMessage());
                }
            });
        }

        ImageView parentImg = view.findViewById(R.id.parent_psw_img);
        ImageView fingerIconImg = view.findViewById(R.id.parent_finger_icon_img);
        final TextView hintText = view.findViewById(R.id.parent_dialog_hint_tv);
        hintText.setVisibility(View.GONE);
        if(mFingerprintIdentify.isFingerprintEnable()
                && mFingerprintIdentify.isHardwareEnable()
                && mFingerprintIdentify.isRegisteredFingerprint()){

            parentImg.setVisibility(View.GONE);
            fingerIconImg.setVisibility(View.VISIBLE);

            ((TextView)view.findViewById(R.id.parent_dialog_tv)).setText("请验证指纹或者家长密码进入");

            mFingerprintIdentify.startIdentify(5, new BaseFingerprint.FingerprintIdentifyListener() {
                @Override
                public void onSucceed() {
                    vercodeLayout.setVisibility(View.GONE);
                    KeyboardUtils.hideKeyboard(vercodeLayout);

                    boolean isNotFirstParent = SharedPreferencesUtils.getBooleanDate(Constants.APP_NOT_FIRST_PARENT_SHAREPREFERS_KEY);
                    if(!isNotFirstParent){
                        startActivity(ParentGuideActivity.class);
                    }
                }

                @Override
                public void onNotMatch(int availableTimes) {
                    hintText.setVisibility(View.VISIBLE);
                    hintText.setText("指纹不匹配，还剩余" + availableTimes + "次机会");
                    Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
                    hintText.startAnimation(shake);

                }

                @Override
                public void onFailed(boolean isDeviceLocked) {
                    Log.i(TAG,"onFailed " + isDeviceLocked);
                    hintText.setText("指纹尝试过多，请使用家长密码");
                    Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
                    hintText.startAnimation(shake);
                }

                @Override
                public void onStartFailedByDeviceLocked() {
                    Log.i(TAG,"onStartFailedByDeviceLocked ");
                    hintText.setText("指纹尝试过多，请使用家长密码");
                    Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
                    hintText.startAnimation(shake);
                }
            });
        }else {
            parentImg.setVisibility(View.VISIBLE);
            fingerIconImg.setVisibility(View.GONE);
            ((TextView)view.findViewById(R.id.parent_dialog_tv)).setText("请输入家长密码进入");
        }

        if(veriCodeView != null){
            veriCodeView.clear();
        }
    }

    @Override
    public void onComplete(String content) {
        if(StringTools.pswValide(content)){
            vercodeLayout.setVisibility(View.GONE);
            KeyboardUtils.hideKeyboard(vercodeLayout);

            boolean isNotFirstParent = SharedPreferencesUtils.getBooleanDate(Constants.APP_NOT_FIRST_PARENT_SHAREPREFERS_KEY);
            if(!isNotFirstParent){
                startActivity(ParentGuideActivity.class);
            }
        }else {
            showToast("家长密码错误");
            if(veriCodeView != null){
                veriCodeView.clear();
            }
        }
    }
}
